import numpy as np
import time

class GaussianElimination:
    def __init__(self):
        self.execution_time = 0.0

    def partial_pivot(self, A, b):
        n = len(b)

        start_time = time.time()

        for k in range(n - 1):
            max_index = np.argmax(np.abs(A[k:, k:]))
            max_row, max_col = divmod(max_index, n - k)
            max_row += k

            A[[k, max_row]] = A[[max_row, k]]
            b[[k, max_row]] = b[[max_row, k]]

            for i in range(k + 1, n):
                multiplier = -A[i, k] / A[k, k]
                A[i, k:] += multiplier * A[k, k:]
                b[i] += multiplier * b[k]

        x = np.zeros(n)
        for i in range(n - 1, -1, -1):
            x[i] = (b[i] - np.dot(A[i, i+1:], x[i+1:])) / A[i, i]

        end_time = time.time()
        self.execution_time = end_time - start_time

        return x

# 使用示例
n = 8000
A = np.random.rand(n, n)
b = np.random.rand(n)

gaussian_elimination = GaussianElimination()
x = gaussian_elimination.partial_pivot(A, b)

print("解向量 x:", x)
print("执行时间:", gaussian_elimination.execution_time, "秒")
